import re
import time

import requests
from appium import webdriver
from appium.webdriver.common.appiumby import  AppiumBy
from selenium.webdriver import Keys
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
import appium

import os
import settings
from base.base import Base


class RegisterPage():

    def __init__(self, driver):
        self.personal_homepage_locator = (AppiumBy.XPATH,
"//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.widget.ImageView[1]")  # 个人页面元素

        self.subscription_management_locator = (AppiumBy.XPATH,
"//android.widget.ImageView[@content-desc='订阅管理']")  # 订阅管理元素

        self.logout_button_locator = (AppiumBy.XPATH,
"//android.widget.ImageView[@content-desc='退出登录']")  # 登出按钮

        self.confirm_logout_locator = (AppiumBy.XPATH,
"//android.widget.Button[@content-desc='退出登录']")  # 确定登出按钮

        self.register_now_button_locator = (AppiumBy.XPATH,
"//android.widget.Button[@content-desc='立即注册']")#立即注册按钮

        self.email_input_locator=(AppiumBy.XPATH,
"//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.widget.EditText[1]")#邮箱输入

        self.enter_password_locator=(AppiumBy.XPATH,
"//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.widget.EditText[1]")#密码输入

        self.confirm_password_locator=(AppiumBy.XPATH,
"//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.widget.EditText[1]")#确认密码输入

        self.register_button_locator=(AppiumBy.XPATH,
"//android.widget.Button[@content-desc='注册']")

        self.ok_button_locator=(AppiumBy.XPATH,
"// android.widget.Button[@ content-desc='Ok']")

        # 接受传入的driver
        self.driver = driver

        # 生成一个显示等待对象
        self.wait = WebDriverWait(driver, 5)

        #调试语句
        #self.driver=webdriver.Remote(settings.appium_server,settings.desired_caps)

        # 获取当前文件目录和上层目录绝对路径
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        parent_dir = os.path.dirname(cur_dir)

        # 实例化base
        self.base = Base(self.driver, self.wait)

    # 点击个人页面操作
    def click_personal_homepage(self) -> None:
        self.base.click_personal_homepage(self.personal_homepage_locator)

    # 点击订阅管理操作
    def click_subscription_management(self) -> None:
        self.base.click_subscription_management(self.subscription_management_locator)

    # 点击登出按钮
    def click_logout_button(self) -> None:
        self.base.click_logout_button(self.logout_button_locator)

    # 点击确认登出按钮
    def click_confirm_logout(self) -> None:
        self.base.click_confirm_logout(self.confirm_logout_locator)

    def click_register_now_button(self) -> None:
        self.base.click_register_now_button(self.register_now_button_locator)

    def email_input(self,email) -> None:
        self.base.email_input(self.email_input_locator, email)

    def enter_password(self,password) -> None:
        self.base.enter_password(self.enter_password_locator, password)

    def re_enter_password(self,re_password) -> None:
        self.base.re_enter_password(self.confirm_password_locator,re_password)

    def click_register(self) -> None:
        self.base.click_register(self.register_button_locator)

    def click_ok_button(self) -> None:
        self.base.click_ok_button(self.ok_button_locator)
#切换环境
    def switch_environment(self) -> None:
        self.driver.tap([(73, 358)], 3000)
        self.driver.tap([(391, 1843)])

        element=(AppiumBy.XPATH,"//android.widget.EditText")
        self.wait.until(expected_conditions.presence_of_element_located(element))
        self.driver.find_element(*element).send_keys("356478")#输入密码

        self.driver.press_keycode(66)#输入回车键

        element1=(AppiumBy.XPATH,"//android.widget.Button[@content-desc='切换Url']")
        self.wait.until(expected_conditions.presence_of_element_located(element1))
        self.driver.find_element(*element1).click()

        element2 = (AppiumBy.XPATH, "//android.view.View[@content-desc='url: https://testope.ope.ai']")
        self.wait.until(expected_conditions.presence_of_element_located(element2))
        self.driver.find_element(*element2).click()

        element3 = (AppiumBy.XPATH, "//android.widget.Button[@content-desc='确认']")
        self.wait.until(expected_conditions.presence_of_element_located(element3))
        self.driver.find_element(*element3).click()

        self.driver.launch_app()


#激活邮件
    def activation_email(self)-> None:
        response = requests.get("https://api.guerrillamail.com/ajax.php?f=get_email_address&lang=en&site=guerrillamail.com&ip=127.0.0.1&agent=Mozilla_foo_bar")
        # 从响应中提取邮箱地址和 sid_token
        email_data = response.json()
        email_address = email_data.get("email_addr")
        sid_token = email_data.get("sid_token")
        print(f"临时邮箱地址: {email_address}")
        print(f"sid_token: {sid_token}")
        print("Random Email:", email_address)
        self.base.email_input(self.email_input_locator, email_address)
        self.base.enter_password(self.enter_password_locator, "X11111")
        self.base.re_enter_password(self.confirm_password_locator, "X11111")
        self.base.click_register(self.register_button_locator)
        #获取邮件
        email_check = requests.get(f"https://api.guerrillamail.com/ajax.php?f=get_email_list&offset=0&sid_token={sid_token}")
        email_id = email_check.json()['list'][0]['mail_id']
        print(email_id)
        #读取邮件
        email_read = requests.get(f"https://api.guerrillamail.com/ajax.php?f=fetch_email&sid_token={sid_token}&email_id={email_id}")
        mail_body = email_read.json()['mail_body']
        print(mail_body)
        time.sleep(10)

        pattern = r'key=([a-fA-F0-9]+)'
        match = re.search(pattern, mail_body)
        base_url = 'https://testope.ope.ai/api/login/active'
        key = mail_body
        print(key)
        response = requests.get(base_url + "?key=" + key)

        if response.status_code == 200:
            data = response.json()
            print(data)
        else:
            print(f"Failed to access the API. Status code: {response.status_code}")
            print(response.text)



if __name__ == '__main__':
    driver = webdriver.Remote(settings.appium_server, settings.desired_caps)
    # register_page = RegisterPage(driver)
    # register_page.click_personal_homepage()
    # register_page.click_logout_button()
    # register_page.click_confirm_logout()
    # register_page.click_personal_homepage()
    # register_page.click_subscription_management()
    # register_page.switch_environment()
    # register_page.activation_email()





